import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import axios from 'axios'

export const createStore = () => {
  return new Vuex.Store({
    state: {
      // posts: [{name:"xx",age:0}]
      posts: []
    },
    mutations: {
      setPosts(state, posts = []) {
          state.posts = posts
      }
    },
    actions: {
      //服务端渲染必须让actions返回一个promise
      async getPosts({ commit }) {
        const { data } = await axios.get('https://cnodejs.org/api/v1/topics')
        commit('setPosts', data.data)
      }
    }
  })
}